package com.xq.hhl.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.content.Context;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler
{
    // APP_ID 替换为你的应用从官方网站申请到的合法appID
    public static String APP_ID = "123";

    // IWXAPI 是第三方 app 和微信通信的 openApi 接口
    private static IWXAPI API;
    public static int State = 0;
    public static String Result = "未知";

    public static void Auth(Context context)
    {
        State = 1;
        RegistToWX(context);

        SendAuth.Req req = new SendAuth.Req();
        //应用授权作用域，如获取用户个人信息则填写 snsapi_userinfo
        req.scope = "snsapi_userinfo";
        //用于保持请求和回调的状态，授权请求后原样带回给第三方
        req.state = "wechat_sdk_auth";
        API.sendReq(req);
    }
    public static void Share(Context context)
    {
        State = 1;
        RegistToWX(context);

    }
    private static void RegistToWX(Context context)
    {
        if(API == null)
        {
            API = WXAPIFactory.createWXAPI(context, APP_ID, true);
            API.registerApp(APP_ID);
        }
    }

    // IWXAPI 是第三方 app 和微信通信的 openApi 接口
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, APP_ID, false);
        try {
            api.handleIntent(getIntent(), this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }
    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req)
    {

    }
    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp resp)
    {
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK://用户同意
                SendAuth.Resp newResp = (SendAuth.Resp)resp;
                State = 2;
                Result = newResp.code;
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消
                State = -1;
                Result = "用户取消";
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED://用户拒绝
                State = -2;
                Result = "用户拒绝";
                break;
            case BaseResp.ErrCode.ERR_UNSUPPORT://不支持
                State = -3;
                Result = "不支持";
                break;
            default://未知
                State = -4;
                Result = "未知";
                break;
        }
        finish();
    }
}